package com.nd.teamfile.fileexplorer.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nd.teamfile.fileexplorer.R;
import com.nd.teamfile.fileexplorer.sdk.type.FileInfo;

/**
 * ClassName:VersionResumeAdapter Function: 版本履历使用的适配器 Reason: TODO ADD REASON
 * 
 * @author linqm
 * @version
 * @since Ver 1.1
 * @Date 2013 2013-12-18 下午6:00:25
 * 
 * @see
 */
public class FMLocalFileAdapter extends BaseAdapter {
	private ArrayList<FileInfo> mFilelist = null;
	private Context mContext = null;

	public FMLocalFileAdapter(Context context, ArrayList<FileInfo> filelist) {
		mContext = context;
		mFilelist = filelist;
	}

	@Override
	public int getCount() {
		return mFilelist.size();
	}

	@Override
	public Object getItem(int position) {
		if (mFilelist == null)
			return null;
		else
			return mFilelist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		FMFileItem versionItem;
		if (convertView == null) {
			versionItem = new FMFileItem();
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.fmlocalfile_listview_item, null);
			versionItem.FMLinearLayout = (RelativeLayout) convertView
					.findViewById(R.id.FMLayout);
			versionItem.FMNameText = (TextView) convertView
					.findViewById(R.id.FMName);
			versionItem.FMImageView = (ImageView) convertView
					.findViewById(R.id.FMLogo);
			convertView.setTag(versionItem);
		} else {
			versionItem = (FMFileItem) convertView.getTag();
		}
		versionItem.FMImageView.setImageResource(mFilelist.get(position)
				.getState());
		versionItem.FMNameText.setText(mFilelist.get(position).getName());
		if (mFilelist.size() == 1) {
			versionItem.FMLinearLayout
					.setBackgroundResource(R.drawable.preference_one_item);
		} else {
			if (position == 0) {
				versionItem.FMLinearLayout
						.setBackgroundResource(R.drawable.preference_first_item);
			} else if (position == mFilelist.size() - 1) {
				versionItem.FMLinearLayout
						.setBackgroundResource(R.drawable.preference_last_item);
			} else {
				versionItem.FMLinearLayout
						.setBackgroundResource(R.drawable.preference_item);
			}
		}

		return convertView;
	}

	public final class FMFileItem {
		public RelativeLayout FMLinearLayout;
		public TextView FMNameText;
		public ImageView FMImageView;
	}

}
